home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_434 / backup / datetos.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  2KB  |  77 lines

  1.  
  2. /*
  3.  *  DATETOS.C
  4.  *
  5.  *  datetos(date, str, ctl)
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <libraries/dos.h>
  11. #include <libraries/dosextens.h>
  12.  
  13. typedef struct DateStamp    DATESTAMP;
  14.  
  15. static char dim[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  16. static char *Month[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul",
  17.                "Aug","Sep","Oct","Nov","Dec" };
  18.  
  19. char *
  20. datetos(date, str, ctl)
  21. DATESTAMP *date;
  22. char *str;
  23. char *ctl;
  24. {
  25.     long days, years;
  26.     short leap, month;
  27.  
  28.     if (ctl == NULL)
  29.     ctl = "D M Y h:m:s";
  30.     days = date->ds_Days + 731;         /*    1976 (ly)       */
  31.     years = days / (366+365*3);             /*  #quad yrs       */
  32.     days -= years * (366+365*3);            /*  days remaining  */
  33.                         /*    0 = jan 1    */
  34.     leap = (days <= 365);                   /*  0-365, is a leap yr */
  35.     years = 1976 + 4 * years;            /*    base yr     */
  36.     if (leap == 0) {                        /*  days >= 366     */
  37.     days -= 366;                /*    add a year    */
  38.     ++years;
  39.     years += days / 365;            /*    non-lyrs left    */
  40.     days  %= 365;                /*    0-364        */
  41.     }
  42.     for (month = 0; (month==1) ? (days >= 28 + leap) : (days >= dim[month]); ++month)
  43.     days -= (month==1) ? (28 + leap) : dim[month];
  44.     {
  45.     register short i = 0;
  46.     for (; *ctl; ++ctl) {
  47.         switch(*ctl) {
  48.         case 'h':
  49.         sprintf(str+i, "%02d", date->ds_Minute / 60);
  50.         break;
  51.         case 'm':
  52.         sprintf(str+i, "%02d", date->ds_Minute % 60);
  53.         break;
  54.         case 's':
  55.         sprintf(str+i, "%02d", date->ds_Tick / 50 % 60);
  56.         break;
  57.         case 'Y':
  58.         sprintf(str+i, "%ld", years);
  59.         break;
  60.         case 'M':
  61.         strcpy(str+i, Month[month]);
  62.         break;
  63.         case 'D':
  64.         sprintf(str+i,"%2ld", days+1);
  65.         break;
  66.         default:
  67.         str[i] = *ctl;
  68.         str[i+1] = 0;
  69.         break;
  70.         }
  71.         i += strlen(str+i);
  72.     }
  73.     }
  74.     return(str);
  75. }
  76.  
  77.